package com.letian.web.controller.im;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.letian.common.core.domain.entity.SysUser;
import com.letian.common.core.domain.model.UsersVO;
import com.letian.im.domain.model.UsersListInMessageVO;
import com.letian.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.im.domain.ImFriend;
import com.letian.im.service.IImFriendService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 朋友信息Controller
 * 
 * @author bmz
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/im/friend")
public class ImFriendController extends BaseController
{
    @Autowired
    private IImFriendService imFriendService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询朋友信息列表
     */
    @PreAuthorize("@ss.hasPermi('im:friend:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImFriend imFriend)
    {
        startPage();
        List<ImFriend> list = imFriendService.selectImFriendList(imFriend);
        return getDataTable(list);
    }

    /**
     * 查询在相同群组内的其他用户
     */
    @PreAuthorize("@ss.hasPermi('im:friend:list')")
    @GetMapping("/queryUserListInSameGroup")
    public AjaxResult queryUserListInSameGroup(String nickName)
    {
        List<UsersVO> list = imFriendService.queryUserListInSameGroup(getUserId(),nickName);
        return success(list);
    }

    /**
     * 查询在最久通讯的用户列表
     */
    @PreAuthorize("@ss.hasPermi('im:friend:list')")
    @GetMapping("/queryUserListInMessage")
    public AjaxResult queryUserListInMessage()
    {
        List<UsersListInMessageVO> list = imFriendService.queryUserListInMessage(getUserId());
        return success(list);
    }

    /**
     * 查询在最久通讯的用户列表
     */
    @PreAuthorize("@ss.hasPermi('im:friend:query')")
    @GetMapping("/queryFriendUserInfo/{userId}")
    public AjaxResult queryFriendUserInfo(@PathVariable("userId")  Long userId)
    {
        SysUser user = userService.selectUserById(userId);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(user, usersVO);
        return success(usersVO);
    }

    /**
     * 导出朋友信息列表
     */
    @PreAuthorize("@ss.hasPermi('im:friend:export')")
    @Log(title = "朋友信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImFriend imFriend)
    {
        List<ImFriend> list = imFriendService.selectImFriendList(imFriend);
        ExcelUtil<ImFriend> util = new ExcelUtil<ImFriend>(ImFriend.class);
        util.exportExcel(response, list, "朋友信息数据");
    }

    /**
     * 获取朋友信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('im:friend:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imFriendService.selectImFriendById(id));
    }

    /**
     * 新增朋友信息
     */
    @PreAuthorize("@ss.hasPermi('im:friend:add')")
    @Log(title = "朋友信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImFriend imFriend)
    {
        return toAjax(imFriendService.insertImFriend(imFriend));
    }

    /**
     * 修改朋友信息
     */
    @PreAuthorize("@ss.hasPermi('im:friend:edit')")
    @Log(title = "朋友信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImFriend imFriend)
    {
        return toAjax(imFriendService.updateImFriend(imFriend));
    }

    /**
     * 删除朋友信息
     */
    @PreAuthorize("@ss.hasPermi('im:friend:remove')")
    @Log(title = "朋友信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imFriendService.deleteImFriendByIds(ids));
    }
}
